var backstageTime;//后台时间
var username;//用户名
var openId;//
$(function () {
    $.get("/getTime.do",function (data) {
        if(data.code == 200){
            backstageTime = data.data;
        }else{
            alert("系统获取时间出错，请联系管理员")
        }
    });
    setInterval(getDate, 1000);
    setInterval(getList, 1000 * 10);
    getUser();
})
var oSpan = $("#dataTime");
function tow(n) {
    return n >= 0 && n < 10 ? '0' + n : '' + n;
}
function getDate() {
    var oDate = new Date();//获取日期对象
    var oldTime = oDate.getTime();//现在距离1970年的毫秒数
    var newDate = new Date(backstageTime);
    var newTime = newDate.getTime();//2019年距离1970年的毫秒数
    var second = Math.floor((newTime - oldTime) / 1000);//未来时间距离现在的秒数
    if(second == 0){
        //倒计时结束，执行跳转
        toActivityPage();
    }
    if(second < 0){
        alert("活动已结束");
        return;
    }
    var day = Math.floor(second / 86400);//整数部分代表的是天；一天有24*60*60=86400秒 ；
    second = second % 86400;//余数代表剩下的秒数；
    var hour = Math.floor(second / 3600);//整数部分代表小时；
    second %= 3600; //余数代表 剩下的秒数；
    var minute = Math.floor(second / 60);
    second %= 60;
    var str = tow(day) + '<span class="time">天</span>'
        + tow(hour) + '<span class="time">小时</span>'
        + tow(minute) + '<span class="time">分钟</span>'
        + tow(second) + '<span class="time">秒</span>';
    oSpan.html(str);
}
function getList(){
    var url = "/getManList.do";
    var param = {};
    $.get(url,param,function(data){
        if (data.code != 200) {
            alert("获取用户列表错误，请联系管理员");
        } else {
            let item = data.data;
            let userDom = document.getElementById('userImg');
            let userBox = [];
            for (let i = 0,len = item.length; i< len; i += 4) {
                    userBox.push(`<footer class=\"user-img display-flex flex-al-ct flex-sp-ar\">
                            ${item[i] ? getSection(item[i]) : ''}
                            ${item[i + 1] ? getSection(item[i + 1]) : ''}
                            ${item[i + 2] ? getSection(item[i + 2]) : ''}
                            ${item[i + 3] ? getSection(item[i + 3]) : ''}
                            </footer>`)
            }
    //         for (let i = 0,len = 8; i< len; i += 4) {
    //             userBox.push(`<footer class=\"user-img display-flex flex-al-ct flex-sp-ar\">
    //                 ${getSection(0)}
    //                 ${getSection(1)}
    //         ${getSection(2)}
    // ${getSection(3)}
    //                         </footer>`)
    //         }
            userDom.innerHTML = userBox.join('');
        }
    });
}
function getSection(item){
    return `<!--<section class="join-user-img display-flex"><img src="/images/nvren.svg"/><p>12uygugiugugihhbgjhhb3</p></section>-->`;
    if(item == null) return `<section></section>`;
    if(item.gender == 'FEMALE'){
        return `<section class="join-user-img display-flex"><img src="/images/nvren.svg"/><p>${item.username}</p></section>`;
    }else if(item.gender == 'OTHER'){
        return `<section class="join-user-img display-flex"><p>&nbsp;&nbsp;${item.username}&nbsp;&nbsp;</p></section>`;
    }else{
        return `<section class="join-user-img display-flex"><img src="/images/nanren.svg"/><p>${item.username}</p></section>`;
    }
}

function getUser(){
    username = decodeURI(getCookie("username"));
    openId = decodeURI(getCookie("openId"));
    $('#title').append(username);
    getList();
}
function toActivityPage(){
    // window.location.href="/activity?username="+username+"&openId="+openId;
    window.location.href="/activity";
}
//读取cookies
function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name)  == 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
